#include "sortbase.h"
#include <algorithm>
#include <chrono>
#include <random>

SortBase::SortBase(QObject *parent) : QObject(parent) {}

bool SortBase::runState() const
{
    return isRunning;
}

void SortBase::setRunState(bool state)
{
    if (isRunning == state)
        return;

    isRunning = state;
    emit runStateChanged(state);
}

void SortBase::setArray(const std::vector<size_t> &given_arr)
{
    arr.assign(given_arr.begin(), given_arr.end());
}

void SortBase::randomArray(size_t num_of_elem)
{
    arr.resize(num_of_elem);
    for (size_t i = 0; i < num_of_elem; i++) {
        arr[i] = i + 1;
    }

    shuffle();
}

void SortBase::shuffle()
{
    auto seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::shuffle(arr.begin(), arr.end(), std::default_random_engine(seed));

    emit updateRequest();
}
